package com.jt.sp02.controller;

import com.jt.sp01.pojo.Item;
import com.jt.sp02.service.ItemServiceImpl;
import com.jt.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@Slf4j
public class ItemController {
    @Autowired
    private ItemServiceImpl itemService;

    //直接用浏览器访问测试
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable("orderId") String orderId) throws InterruptedException {
        List<Item> items = itemService.getItem(orderId);
        //随机阻塞,90%概率阻塞代码
        if(Math.random()<0.9){
            //随机延迟时长0-5s
            int i = new Random().nextInt(5000);
            log.info("延迟:  " + i);
            Thread.sleep(i);
        }
        return JsonResult.ok().data(items);
    }
    //使用 postman 测试 post请求
    @PostMapping("/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        itemService.decreaseNumber(items);
        return JsonResult.ok().msg("减少库存成功");
    }


}
